package com.prawn.trace.record.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Selection;

import com.prawn.trace.record.dao.RecordWarehousingDao;
import com.prawn.trace.record.pojo.RecordTransport;
import com.prawn.trace.record.pojo.RecordWarehousing;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import util.IdWorker;
import util.NullAttributeUtils;


/**
 * 服务层
 * 
 * @author Administrator
 *
 */
@Service
public class RecordWarehousingService {

	@Autowired
	private RecordWarehousingDao recordWarehousingDao;
	
	@Autowired
	private IdWorker idWorker;

	/**
	 * 查询全部列表
	 * @return
	 */
	public List<RecordWarehousing> findAll() {
		Sort sort=new Sort(Sort.Direction.DESC,"createDate");
		return recordWarehousingDao.findAll(sort);
	}

	
	/**
	 * 条件查询+分页
	 * @param whereMap
	 * @param page
	 * @param size
	 * @return
	 */
	public Page<RecordWarehousing> findSearch(Map whereMap, int page, int size) {
		Specification<RecordWarehousing> specification = createSpecification(whereMap);
		Sort sort=new Sort(Sort.Direction.DESC,"createDate");
		PageRequest pageRequest =  PageRequest.of(page-1, size,sort);
		return recordWarehousingDao.findAll(specification, pageRequest);
	}

	
	/**
	 * 条件查询
	 * @param whereMap
	 * @return
	 */
	public List<RecordWarehousing> findSearch(Map whereMap) {
		Sort sort=new Sort(Sort.Direction.DESC,"createDate");
		Specification<RecordWarehousing> specification = createSpecification(whereMap);
		return recordWarehousingDao.findAll(specification,sort);
	}

	/**
	 * 根据ID查询实体
	 * @param id
	 * @return
	 */
	public RecordWarehousing findById(String id) {
		return recordWarehousingDao.findById(id).get();
	}

	/**
	 * 增加
	 * @param recordWarehousing
	 */
	public void add(RecordWarehousing recordWarehousing) {
		recordWarehousing.setId( idWorker.nextId()+"" );
		recordWarehousingDao.save(recordWarehousing);
	}

	/**
	 * 修改
	 * @param recordWarehousing
	 */
	public void update(RecordWarehousing recordWarehousing) {
		RecordWarehousing oldRecordWarehousing =recordWarehousingDao.findById(recordWarehousing.getId()).get();
		BeanUtils.copyProperties(recordWarehousing,oldRecordWarehousing, NullAttributeUtils.getNullPropertyNames(recordWarehousing));
		recordWarehousingDao.save(oldRecordWarehousing);
	}

	/**
	 * 删除
	 * @param id
	 */
	public void deleteById(String id) {
		recordWarehousingDao.deleteById(id);
	}

	/**
	 * 动态条件构建
	 * @param searchMap
	 * @return
	 */
	private Specification<RecordWarehousing> createSpecification(Map searchMap) {

		return new Specification<RecordWarehousing>() {

			@Override
			public Predicate toPredicate(Root<RecordWarehousing> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicateList = new ArrayList<Predicate>();
                // ID
                if (searchMap.get("id")!=null && !"".equals(searchMap.get("id"))) {
                	predicateList.add(cb.like(root.get("id").as(String.class), "%"+(String)searchMap.get("id")+"%"));
                }
                // 仓库名称（引用场地信息）
                if (searchMap.get("name")!=null && !"".equals(searchMap.get("name"))) {
                	predicateList.add(cb.like(root.get("name").as(String.class), "%"+(String)searchMap.get("name")+"%"));
                }
                // 负责人
                if (searchMap.get("responsiblePerson")!=null && !"".equals(searchMap.get("responsiblePerson"))) {
                	predicateList.add(cb.like(root.get("responsiblePerson").as(String.class), "%"+(String)searchMap.get("responsiblePerson")+"%"));
                }
                // 检验结果
                if (searchMap.get("checkResult")!=null && !"".equals(searchMap.get("checkResult"))) {
                	predicateList.add(cb.like(root.get("checkResult").as(String.class), "%"+(String)searchMap.get("checkResult")+"%"));
                }
                // 仓库环境
                if (searchMap.get("environment")!=null && !"".equals(searchMap.get("environment"))) {
                	predicateList.add(cb.like(root.get("environment").as(String.class), "%"+(String)searchMap.get("environment")+"%"));
                }
                // 产品批次ID
                if (searchMap.get("produceBatchId")!=null && !"".equals(searchMap.get("produceBatchId"))) {
                	predicateList.add(cb.like(root.get("produceBatchId").as(String.class), "%"+(String)searchMap.get("produceBatchId")+"%"));
                }
                // 生产流程
                if (searchMap.get("processId")!=null && !"".equals(searchMap.get("processId"))) {
                	predicateList.add(cb.like(root.get("processId").as(String.class), "%"+(String)searchMap.get("processId")+"%"));
                }
                // 创建者
                if (searchMap.get("createBy")!=null && !"".equals(searchMap.get("createBy"))) {
                	predicateList.add(cb.equal(root.get("createBy").as(String.class), (String)searchMap.get("createBy")));
                }
				
				return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));

			}
		};

	}

}
